package com.hcb.tonguetiphealth.widget.more;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.util.HttpUtils;
import com.hcb.tonguetiphealth.util.MessageWhat;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.widget.BottomWidget;
import com.hcb.tonguetiphealth.widget.GeneralWidget;

public class AdviceWidget extends GeneralWidget implements OnClickListener {
	private static final String TAG = "InfoWidget";

	private Activity ctx;

	private LinearLayout llBack;
	private Button btCommit;
	private EditText etAdvice;
	private Handler mHandler;
	private boolean commitSuccess = false;


	public AdviceWidget(Context context) {
		super(context);
		ctx = (Activity) context;
		inflate(context, R.layout.more_advice, this);

		new BottomWidget(context, this);
		bindView();
		bindEvent();
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case MessageWhat.SHOW_DIALOG:
					showMessageDialog(ctx, "正在提交数据...");
					break;
				case MessageWhat.DIMISS_DIALOG:
					dismissMessageDialog();
					if (commitSuccess) {
						showMessage(ctx, "提交成功，感谢您的参与！");
					}else {
						showMessage(ctx, "提交失败，请稍后重试！");
					}
					break;
				default:
					break;
				}
			}
		};

	}

	private void bindView() {
		llBack = (LinearLayout) findViewById(R.id.more_advice_back);
		btCommit = (Button)findViewById(R.id.more_advice_commit);
		etAdvice = (EditText)findViewById(R.id.more_advice_content);
	}

	private void bindEvent() {
		llBack.setOnClickListener(this);
		btCommit.setOnClickListener(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.more_advice_back:
			ctx.setContentView(new MoreWidget(ctx));
			break;
		case R.id.more_advice_commit:
			String strAdvice = etAdvice.getText().toString().trim();
			if (strAdvice == null || strAdvice.equals("")) {
				showMessage(ctx, "给点意见吧！");
				return;
			}
			commitAdvice(strAdvice);
			break;
		default:
			break;
		}
	}

	private void commitAdvice(final String strAdvice) {
		commitSuccess = false;
		mHandler.sendEmptyMessage(MessageWhat.SHOW_DIALOG);
		new Thread() {
			public void run() {
				
				List <NameValuePair> params = new ArrayList <NameValuePair>();
				params.add(new BasicNameValuePair("advice", strAdvice));
				String respone = HttpUtils.Post(Global.getServletadvice(), params);
				if (Global.DEBUG) {
					System.out.println("response"+respone);
				}
				if (respone != null) {
					try {
						JSONObject jsonObject = new JSONObject(respone);
						String strReturn = jsonObject.getString("return");
						if ("true".equals(strReturn)) {
							commitSuccess = true;
						}
						if (Global.DEBUG) {
							System.out.println("msg:"+jsonObject.getString("msg"));
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} 
				mHandler.sendEmptyMessage(MessageWhat.DIMISS_DIALOG);
			}
		}.start();
	}
	
	@Override
	public void notifyAdapter() {

	}
}
